package com.linxz.utils;

import android.annotation.SuppressLint;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;

/**
 * @author Linxz
 *         time:2018年05月23日 14:13.
 *         时间相关工具类
 */

public class TimeUtils {

    /**
     * 时间转换
     * @param dataFormat:
     * @param timeStamp:
     * @return String
     */
    @SuppressLint("SimpleDateFormat")
    public static String formatData(String dataFormat, long timeStamp) {
        if (timeStamp == 0) {
            return "";
        }
        timeStamp = timeStamp * 1000;
        String result;
        SimpleDateFormat format = new SimpleDateFormat(dataFormat);
        result = format.format(new Date(timeStamp));
        return result;
    }

    /**
     * @param time
     *            时间
     * @param level
     *            参考Calendar
     * @return "yyyy-MM-dd kk:mm:ss" 格式的时间
     */
    @SuppressLint("SimpleDateFormat")
    public static String longToTime(long time, int level) {
        String format = "yyyy-MM-dd HH:mm:ss";
        switch (level) {
            case Calendar.MINUTE: {
                format = "yyyy-MM-dd HH:mm";
            }
            break;
            case Calendar.HOUR: {
                format = "yyyy-MM-dd HH";
            }
            break;
            case Calendar.DATE: {
                format = "yyyy-MM-dd";
            }
            break;
            case Calendar.MONTH: {
                format = "yyyy-MM";
            }
            break;
            case Calendar.YEAR: {
                format = "yyyy";
            }
            break;
            default:
                break;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(time);
    }

    /**
     * @param time
     *            时间
     * @param level
     *            参考Calendar
     * @return "yyyy-MM-dd kk:mm:ss" 格式的时间
     */
    @SuppressLint("SimpleDateFormat")
    public static String longToTime(String time, int level) {
        return longToTime(Long.parseLong(time),level);
    }

    public final static int SLOT_YESTERDAY=-1;
    public final static int SLOT_TODAY=0;
    public final static int SLOT_SEVEN=7;
    public final static int SLOT_MONTH=30;
    public final static int SLOT_ALL=666;
    /**
     * 昨天到今天的日期
     */
    @SuppressLint("SimpleDateFormat")
    public static String[] timeSLot(int flag){
        SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd");
        Date date=new Date();
        String todayString = format.format(date);
        Calendar calendar = new GregorianCalendar();
        switch (flag){
            case SLOT_YESTERDAY:
                calendar.add(Calendar.DATE,-1);
                date=calendar.getTime();
                todayString=format.format(date);
                break;
            case SLOT_TODAY:
                calendar.add(Calendar.DATE,0);
                date=calendar.getTime();
                break;
            case SLOT_SEVEN:
                calendar.add(Calendar.DATE,-6);
                date=calendar.getTime();
                break;
            case SLOT_MONTH:
                calendar.add(Calendar.DATE,-30);
                date=calendar.getTime();
                break;
            case SLOT_ALL:
                try {
                    date=format.parse("1999-01-01");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
        String yesterday=format.format(date);
        return new String[]{yesterday,todayString};
    }



    public static String[] timeSLot(String flag){
        SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd");
        Date date=new Date();
        String todayString = format.format(date);
        Calendar calendar = new GregorianCalendar();
        switch (flag){
            case "昨天":
            case "Yesterday":
                calendar.add(Calendar.DATE,-1);
                date=calendar.getTime();
                todayString=format.format(date);
                break;
            case "今天":
            case "Today":
                calendar.add(Calendar.DATE,0);
                date=calendar.getTime();
                break;
            case "近七天":
            case "A week":
                calendar.add(Calendar.DATE,-6);
                date=calendar.getTime();
                break;
            case "近一个月":
            case "A month":
                calendar.add(Calendar.DATE,-30);
                date=calendar.getTime();
                break;
            case "全部":
            case "All":
                try {
                    date=format.parse("1999-01-01");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
        String yesterday=format.format(date);
        return new String[]{yesterday,todayString};
    }



    /**
     * 两个时间戳相差的时分秒
     *
     * */
    public static HashMap<String,String> timeSlotHoursMinuteSecond(long endTime, long starTime) {
        HashMap<String,String> timesMap=new HashMap<>(3);
        if (starTime>=endTime){
            timesMap.put("hourString","00");
            timesMap.put("minuteString","00");
            timesMap.put("secondString","00");
            return timesMap;
        }
        long second=(endTime-starTime)/1000;
        long hours = second / 3600;
        long minutes = (second%3600)/ 60;
        long seconds = second % 60;

        String hourString;
        String minuteString;
        String secondString;
        if(hours < 10){
            hourString = "0";
            if(hours == 0){
                hourString += "0";
            }else{
                hourString += String.valueOf(hours);
            }
        }else{
            hourString = String.valueOf(hours);
        }
        if (minutes < 10) {
            minuteString = "0";
            if (minutes == 0) {
                minuteString += "0";
            }else {
                minuteString += String.valueOf(minutes);
            }
        }else{
            minuteString = String.valueOf(minutes);
        }
        if(seconds < 10){
            secondString = "0";
            if (seconds == 0) {
                secondString += "0";
            }else {
                secondString += String.valueOf(seconds);
            }
        }else {
            secondString = String.valueOf(seconds);
        }

        timesMap.put("hourString",hourString);
        timesMap.put("minuteString",minuteString);
        timesMap.put("secondString",secondString);
        return timesMap;
    }


}
